package com.cloudfun.msglisten.ws.haier.handler;

import com.alibaba.fastjson.JSONObject;
import com.cloudfun.campusshare.RemoteDeviceService;
import com.cloudfun.msglisten.entity.HaierMessage;
import com.cloudfun.msgsubscribe.DeviceInfo;
import com.cloudfun.msgsubscribe.RemoteMessageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 * Created by Huqin on 2020/3/30
 */
@Slf4j
public abstract class AbstractDeviceMsgHandler implements DeviceMsgHandler {

    @Reference(timeout = 3_000)
    protected RemoteDeviceService remoteDeviceService;

//    @Reference(timeout = 3_000)
//    protected RemoteMessageService remoteMessageService;

    @Autowired
    protected RabbitTemplate rabbitTemplate;

    @Autowired
    @Qualifier("externalRabbitTemplate")
    protected RabbitTemplate externalRabbitTemplate;

    @Override
    public void handle(HaierMessage haierMessage) {
        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setTimestamp(haierMessage.getTimestamp());
        deviceInfo.setImei(haierMessage.getImei());
        JSONObject jsonObject = JSONObject.parseObject(haierMessage.getArgs());
        String appId = remoteDeviceService.getAppIdByImei(deviceInfo.getImei());
        this.doHandle(jsonObject, deviceInfo, appId);
    }

    protected abstract void doHandle(JSONObject jsonObject, DeviceInfo deviceInfo, String appId);
}